<!--
订购教材 管理页面
-->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="80px">
      <el-form-item label="选择班级" prop="deptId">
        <treeselect v-model="queryParams.deptId" :options="deptOptions" placeholder="请选择归属部门"
                    style="width: 200px;"/>
      </el-form-item>
      <el-form-item label="归属学年" prop="semesterYear">
        <el-date-picker
          v-model="queryParams.semesterYear"
          type="year"
          value-format="yyyy"
          placeholder="选择学年">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="学期" prop="semester">
        <el-select v-model="queryParams.semester" placeholder="请选择" clearable style="width: 240px">
          <el-option
            v-for="item in dict.type.semester"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['uniquefees:book:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['uniquefees:book:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['uniquefees:book:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['uniquefees:book:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bookList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="课程" align="center" prop="courseId"/>
      <el-table-column label="课程信息" align="center" prop="courseName"/>
      <el-table-column label="教材信息" align="center" prop="courseBook"/>
      <el-table-column label="单价" align="center" prop="price">
        <template slot-scope="scope">
          {{ scope.row.price / 100 }}
        </template>
      </el-table-column>
      <el-table-column label="课程性质" align="center" prop="courseType"/>
      <el-table-column label="学期年" align="center" prop="semesterYear"/>
      <el-table-column label="学期" align="center" prop="semester">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.semester" :value="scope.row.semester"/>
        </template>
      </el-table-column>
      <el-table-column label="班级" align="center" key="deptName" prop="dept.deptName"/>
      <!--      <el-table-column label="缴费说明" align="center" prop="remark" />-->
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['uniquefees:book:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['uniquefees:book:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改教材信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学期" prop="semesterYear" style="margin-right: 10px;">
          <el-col :span="8">
            <el-date-picker style="width: 100px;" v-model="form.semesterYear" type="year" value-format="yyyy"
                            placeholder="选择学年"/>
          </el-col>
          <el-col class="line" :span="8"></el-col>
          <el-col :span="12">
            <el-radio-group v-model="form.semester">
              <el-radio label="C">春季</el-radio>
              <el-radio label="Q">秋季</el-radio>
            </el-radio-group>
          </el-col>
        </el-form-item>
        <el-form-item label="班级" prop="deptId">
          <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true" placeholder="请选择归属部门"/>
        </el-form-item>
        <el-form-item label="课程信息" prop="courseName">
          <el-input v-model="form.courseName" placeholder="请输入课程信息"/>
        </el-form-item>
        <el-form-item label="教材信息" prop="courseBook">
          <el-input v-model="form.courseBook" placeholder="请输入教材信息"/>
        </el-form-item>
        <el-form-item label="单价 (元)" prop="price">
          <el-input v-model="priceInYuan" type="text" placeholder="请输入价格"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.value">
              {{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listBook, getBook, delBook, addBook, updateBook} from "@/api/uniquefees/book";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {deptSchoolTreeSelect} from "@/api/campus/Org";

export default {
  name: "Book",
  components: {Treeselect},
  dicts: ['sys_normal_disable', 'semester'],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 教材信息表格数据
      bookList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 部门树选项
      deptOptions: undefined,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        courseName: null,
        courseBook: null,
        price: null,
        courseType: null,
        semesterYear: new Date().getFullYear().toString(),
        semester: null,
        deptId: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        courseName: [
          {required: true, message: "课程信息不能为空", trigger: "blur"}
        ],
        courseBook: [
          {required: true, message: "教材信息不能为空", trigger: "blur"}
        ],
        price: [
          {required: true, message: "单价不能为空", trigger: "blur"}
        ],
        priceInYuan: [
          {required: true, message: '请输入价格', trigger: 'blur'},
          {
            pattern: /^\d{0,8}(\.\d{0,2})?$/,
            message: '请输入有效的价格（最多8位整数和2位小数）',
            trigger: ['blur', 'change']
          }
        ],
        semesterYear: [
          {required: true, message: "学期年不能为空", trigger: "blur"}
        ],
        semester: [
          {required: true, message: "学期(c 春季学期 q秋季学期)不能为空", trigger: "blur"}
        ],
        deptId: [
          {required: true, message: "班级id不能为空", trigger: "blur"}
        ],
      }
    };
  },
  computed: {
    // 以元为单位的价格，用于表单展示和输入
    priceInYuan: {
      get() {
        return (this.form.price / 100).toFixed(2);
      },
      set(value) {
        this.form.price = Math.round(value * 100);
      }
    }
  },
  created() {
    this.getDeptTree();
    this.getList();
  },
  methods: {
    /** 查询教材信息列表 */
    getList() {
      this.loading = true;
      listBook(this.queryParams).then(response => {
        this.bookList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptSchoolTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        courseId: null,
        courseName: null,
        courseBook: null,
        price: null,
        courseType: null,
        semesterYear: new Date().getFullYear().toString(),
        semester: 'C',
        deptId: null,
        remark: null,
        status: '0',
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.$refs["queryForm"].validate(valid => {
        if (valid) {
          this.queryParams.pageNum = 1;
          this.getList();
        }
      });

    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.courseId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加教材信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const courseId = row.courseId || this.ids
      getBook(courseId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改教材信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.courseId != null) {
            updateBook(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBook(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const courseIds = row.courseId || this.ids;
      this.$modal.confirm('是否确认删除教材信息编号为"' + courseIds + '"的数据项？').then(function () {
        return delBook(courseIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('uniquefees/book/export', {
        ...this.queryParams
      }, `book_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
